iT邦幫忙

2024 iThome 鐵人賽

DAY 4
0

字元和字串
字串解釋成把字元一個一個串起來。
單指char結構本身是指"UTF-16"的Unicode字元,中文稱為萬用字元碼

逸出序列
Char型別的大小是以Unicode 16位元表示字元常值、十六進位逸出序列或是Unicode。所謂逸出序列是指”/”這個特殊字元,緊接在它後面的字元要做特殊處理。
https://ithelp.ithome.com.tw/upload/images/20240918/20121669KnRhp0bBKp.png

String類別建立字串
string 字串變數名稱 = “字串內容”;

string strNull = null;
string strEmpty = String.Empty;
bool isEmpty = {strEmpty == strVacant};
bool isNull = {strNull == strEmpty};

//String(char c, int count);
String ch = new String('-',35);
Console.WriteLine(ch); // 輸出35個字元"-"

字串常用屬性
Chars:取得字串中指定索引位置的字元
Length:取得字串的長度(Char總數)

using System;
using System.Text; // 引入 StringBuilder 所需的命名空間

namespace HelloWorld
{
  class Program
  {
    static void Main(string[] args)
    {
      string word = "This is my favorite programming";
      int index;
      for (index =0; index <=5; index++)
      {
      	Console.WriteLine($"[{index}] = 字元'{word[index]}'");
      }
       Console.WriteLine($"字串總長度 = {word.Length}");
    }
  }
}

字串常用方法
https://ithelp.ithome.com.tw/upload/images/20240918/20121669jgTm1jTIJ6.png

> CompareTo( )

CompareTo(string strB)

string str1 = "abcd";
string str2 = "aacd"; // str1的排序次序在str2之後
int result = str1.CompareTo(str2);//result =1

string str1 = "abcd";
string str2 = "accd"; // str1的排序次序在str2之前
int result = str1.CompareTo(str2);//result =-1

string str1 = "abcd";
string str2 = "ab\u00Adcd"; // str2"ab-cd"
int result = str1.CompareTo(str2);//result =0

> Split()

char[] separ = {',',':'};
string str1 = "Sunday, Monday:Tuesday";
string[] str2 = str1.Split(separ);
foreach(string item in str2)
Console.WriteLine("{0},item");

> InSert( int startIndex, string value);

//在原字串插入其他字串
string str = "Learning programing";
string wds = " Visual C#";
string sentence = str.Insect(str.Length, wds);
Console.WriteLine(sentence);

> Replace(string oldValue, string newValue)

//取代舊文字
string str = "She is a nice girl.";
string wds = "beautiful";
string sentence = str.Replace("nice", wds);
Console.WriteLine(sentence);

> 搜尋字串
IndexOf(string value); ⇒用來搜尋字串的方法,它會回傳指定字串在原始字串中第一次出現的位置。如果找不到指定字串,則回傳 -1。
LastIndexOf(string value); ⇒回傳指定字串最後一次出現所在位置
StartsWish(string value); ⇒字串的開頭
EndsWith(string value); ⇒字串的結尾
Substring(int startIndex); 子字串的起始字元位置

using System;
using System.Text; // 引入 StringBuilder 所需的命名空間

namespace HelloWorld
{
  class Program
  {
    static void Main(string[] args)
    {
		 string str = "Learning programming"; //原始字串
     bool start = str.StartsWith("visual"); 
     Console.WriteLine($"比對字串開頭\"visual\"的結果:{start}");
     bool finish = str.EndsWith("programming"); 
     Console.WriteLine($"比對字串開頭\"programming\"的結果:{finish}");
     int begin = str.IndexOf("g"); //找出字元第一次出現的索引
     Console.WriteLine($"\"g\"開始的索引編號:{begin} ");
     int last = str.LastIndexOf("g");
     Console.WriteLine($"\"g\"最後的索引編號:{last} ");
     string secondStr = str.Substring(begin); //擷取子字串
     Console.WriteLine($"子字串:{secondStr}");
    }
  }
}

以上來源:從零開始學visual C# 2017程式設計、W3Schools、詢問chatGPT
內容有誤的話,請留言或發訊息給我


上一篇
Day3-C#陣列寫法學習Part2
下一篇
Day5-方法和傳遞機制Part1
系列文
初學C#之路:30天基礎學習與實作紀錄10
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言